﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace Compact13MyAppsMenu
{
    public partial class Compact13MyAppsMenu : Form
    {
        public const int formTextAreaHeight = 32;
        public string rootDir { get; set; }
        public Point location { get; set; }
        public Compact13MyAppsMenu()
        {
            InitializeComponent();
            rootDir = "\\temp";
            location = this.location;
        }

        protected  void OnMove(EventArgs ea)
        {
            Invalidate();
        }

        List<System.IO.FileInfo> fileList;

        private void Form1_Load(object sender, EventArgs e)
        {

            if (Program.Args.Length != 0)
            {
                if (Program.Args[0] != "") 
                    rootDir = Program.Args[0];
            }


            this.Text = "Apps in " + rootDir;

            fileList = new List<FileInfo>();
            listBox1.Items.Clear();
            listBox1.Items.Add("Cancel");
            string[] tempdirs;

            //If no leading \\ then will just use one top level directory
            if (rootDir.Substring(0, 1) != "\\")
                tempdirs = new string[] { "\\" + rootDir };
            else
                tempdirs = System.IO.Directory.GetDirectories(rootDir); 
            foreach (string dir in tempdirs)
            {
                if (dir.ToLower() == "\\temp\\debugger")
                    continue;
                if (dir.ToLower() == "\\temp\\compact13myappsmenu")
                    continue;

                string[] files = System.IO.Directory.GetFiles(dir, "*.exe");
                foreach (string fi in files)
                {
                    System.IO.FileInfo fileInfo = new FileInfo(fi);
                    if (fileInfo.Exists)
                    {
                        fileList.Add(fileInfo);
                    }

                }
            }
            var sortedApps =
                    from w in fileList
                    orderby w.Name
                    select w;

            fileList = sortedApps.ToList<FileInfo>();

            if (fileList.Count == 0) 
            {
                MessageBox.Show("No apps in that folder\nSo exiting", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation,MessageBoxDefaultButton.Button1);
                Application.Exit();
            }

            foreach (System.IO.FileInfo fi in fileList)
            {
                listBox1.Items.Add(fi.Name);
                
            }
            int screenHeight = Screen.PrimaryScreen.WorkingArea.Height;
            int listHeight = (listBox1.Items.Count * 161) / 10;
            if ((listHeight + 32) > screenHeight)
                listHeight = screenHeight - formTextAreaHeight;
            this.Height = listHeight + formTextAreaHeight;

            

        }

        private void NotYetImplemented()
        {
            MessageBox.Show("Function not yet implemented", "Compact13Notepad");
        }


        #region FileMenu

        private void menuFile_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemNew_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemOpen_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemSave_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemSaveAs_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemPageSetup_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemPrint_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        #endregion

 
        #region Edit

        private void menuEdit_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }


        private void menuItemUndo_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemCut_Click(object sender, EventArgs e)
        {
            //string str = richTextBox1.SelectedText;
            //Clipboard.SetDataObject( (object) str);
            //richTextBox1.SelectedText = "";
            NotYetImplemented();
        }

        private void menuItemCopy_Click(object sender, EventArgs e)
        {
            //string str = richTextBox1.SelectedText;
            //Clipboard.SetDataObject(str);
            NotYetImplemented();
        }

        private void menuItemPaste_Click(object sender, EventArgs e)
        {
            //IDataObject iData = Clipboard.GetDataObject();

            //// Determines whether the data is in a format you can use. 
            //if (iData.GetDataPresent(DataFormats.Text))
            //{
            //    // Yes it is, so display it in a text box.
            //    richTextBox1.SelectedText = (String)iData.GetData(DataFormats.Text);
            //}
            //else
            //    richTextBox1.SelectedText = "";
            NotYetImplemented();
        }

        private void menuItemDelete_Click(object sender, EventArgs e)
        {
            //richTextBox1.SelectedText = "";
            NotYetImplemented();
            NotYetImplemented();
        }

        private void menuItemFind_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemFindNext_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemReplace_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuGoTo_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemSelectAll_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemTimeDate_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        #endregion
    
        
        #region Help

        private void menuHelp_Click(object sender, EventArgs e)
        {

        }

        private void menuItemHelp_Click(object sender, EventArgs e)
        {
            NotYetImplemented();
        }

        private void menuItemAbout_Click(object sender, EventArgs e)
        {
            AboutBox.ShowAsDialog();
        }
       
        #endregion


       

        private void timer1_Tick(object sender, EventArgs e)
        {
            //this.Location = location;
        }



        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
                return;
            if (listBox1.SelectedIndex != 0)
            {
                FileInfo fi = fileList.ToArray<FileInfo>()[listBox1.SelectedIndex - 1];
                try
                {

                    DialogResult result = Params.ShowAsDialog();
                    switch (result)
                    {
                        case System.Windows.Forms.DialogResult.OK:
                            string args = " " + Params.Args;

                            if (args != "start")
                            {
                                string filename = fi.FullName;
                                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                                proc.EnableRaisingEvents = false;
                                proc.StartInfo.FileName = filename;
                                proc.StartInfo.Arguments = args;
                                proc.Start();
                            }
                            else
                            {
                                //start filename
                                string filename = fi.FullName;
                                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                                proc.EnableRaisingEvents = false;
                                proc.StartInfo.FileName = args;
                                proc.StartInfo.Arguments = filename;
                                proc.Start();
                            }
                            break;
                        case  System.Windows.Forms.DialogResult.Cancel:
                            break;
                    }

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error: Could not run app. Original error: " + ex.Message);
                }
            }
            Application.Exit();

        }






    }
}
